package aom.ju.ss.utils

import android.content.SharedPreferences
import android.text.TextUtils
import androidx.preference.PreferenceManager
import aom.ju.ss.mode.ClassMode
import com.google.gson.Gson
import java.lang.Exception

/**
 * Copyright (©) 2014
 *
 * 1
 * 配置文件读写工具类
 *
 * @author eastonc
 * @version 1.0, 14-7-28 13:43
 * @since 14-7-28
 */
object SpUtils {
    @JvmStatic
    fun putString(keyString: String?, valueString: String?) {
        if (!TextUtils.isEmpty(keyString)) {
            defaultSharedPreferences?.edit()?.putString(keyString, valueString)?.commit()
        }
    }

    @JvmStatic
    fun putString(keyString: String?, valueString: String?, outTime: Long) {//outTime  单位毫秒
        if (!TextUtils.isEmpty(keyString)) {
            val mode = dataMode(System.currentTimeMillis() + outTime)// 过期时间
            mode.data = valueString
            val data = Gson().toJson(mode)
            defaultSharedPreferences?.edit()
                ?.putString(keyString, data)?.commit()
        }
    }

    @JvmStatic
    fun putLong(keyString: String?, valueString: Long) {
        if (!TextUtils.isEmpty(keyString)) {
            defaultSharedPreferences?.edit()?.putLong(keyString, valueString)?.commit()
        }
    }

    @JvmStatic
    fun getLong(keyString: String?): Long {
        return if (!TextUtils.isEmpty(keyString)) {
            return if (defaultSharedPreferences == null) {
                -1
            } else {
                defaultSharedPreferences!!.getLong(keyString, -1L)
            }

        } else -1
    }

    @JvmStatic
    fun putInt(keyString: String?, valueString: Int) {
        if (!TextUtils.isEmpty(keyString)) {
            defaultSharedPreferences?.edit()?.putInt(keyString, valueString)?.commit()
        }
    }

    @JvmStatic
    fun getInt(keyString: String?): Int {
        return if (!TextUtils.isEmpty(keyString)) {
            return if (defaultSharedPreferences == null) {
                -1
            } else {

                defaultSharedPreferences!!.getInt(keyString, -1)
            }
        } else -1
    }

    /**
     * @
     */
    private val defaultSharedPreferences: SharedPreferences?
        get() = PreferenceManager.getDefaultSharedPreferences(BaseApplication.getInstance())

    /**
     * 获取String类型的键值
     */
    @JvmStatic
    fun getString(keyString: String?): String {


        var d = defaultSharedPreferences?.getString(keyString, "")?.let {
            try {
                val mode = Gson().fromJson(it, dataMode::class.java)
                if (mode.time >= System.currentTimeMillis()) {// 过期删除
                    defaultSharedPreferences?.edit()?.remove("keyString")
                    ""
                } else {
                    mode.data!!
                }
            } catch (ex: Exception) {
                it
            }
        }
        if (d == null) {
            d = ""
        }
        return d

    }

    /**
     * 获取String类型的键值
     */
    @JvmStatic
    fun getString(keyString: String?, df: String): String {
        return if (TextUtils.isEmpty(keyString)) {
            ""
        } else if (defaultSharedPreferences == null) {
            ""
        } else {
            defaultSharedPreferences?.getString(
                keyString,
                df
            )!!
        }
    }

    /**
     * 清空键名为 keyString 的键值对
     */
    @JvmStatic
    fun clearKey(keyString: String?) {
        if (!TextUtils.isEmpty(keyString)) defaultSharedPreferences?.edit()?.remove(keyString)
            ?.commit()
    }

    /**
     * 移除键名为 keyString 的键值对
     */
    @JvmStatic
    fun clearAll() {
        defaultSharedPreferences?.edit()?.clear()?.commit()
    }

    class dataMode(var time: Long) {
        var data: String? = ""
            get() {
                if (field == null) {
                    field = ""
                }
                return field
            }
    }
}

